{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8f65272-6ffb-4810-821f-c5fc532c0f84",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "477a2ee6-4df9-42a0-ab0c-deddb021caee",
   "metadata": {},
   "source": [
    "**生成np数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b04fb87c-27f8-4502-bc1e-61326ee04ba6",
   "metadata": {},
   "outputs": [],
   "source": [
    "array = np.array([[2,23,4],\n",
    "                  [2,32,4]],dtype = np.float64) #date type 常用的是 int 和 float，默认使用int64\n",
    "print('dim:',array.ndim)\n",
    "print('shape:',array.shape)\n",
    "print('size:',array.size)\n",
    "print('date type:',array.dtype)\n",
    "print(array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4a02a6d-f624-4b78-b141-551b1278bdf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成特殊数组\n",
    "a = np.zeros((2,3))\n",
    "a = np.ones((2,3)) \n",
    "a = np.empty((2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ba93f19-099c-477e-aaa6-97d6bcfafd55",
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成顺序数组\n",
    "a = np.arange(10,20,2) # 前两个数是左闭右开区间,第一个参数不填的话，默认为0，最后一个数是步长\n",
    "print(a)\n",
    "b = np.arange(20,10,-2)\n",
    "print(b)\n",
    "c = np.arange(12).reshape(3,4)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "722f9528-bdcc-45fe-921e-8ba97b28e4a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成线段\n",
    "a = np.linspace(1,10,6)  #左闭右闭区间，等分5个点（中间插入3个点）\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bc0f525-66f5-41ed-a4f6-e188b868889d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成随机数组\n",
    "a = np.random.random((2,4)) # 第一个random是模块，第二个random是方法，每个数字都是 0~1之间的随机数字\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95e26a25-0c47-431a-9d8b-45229cf04823",
   "metadata": {},
   "source": [
    "**np基础运算**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db366efe-5b52-4a6a-909d-fa328914a29f",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([10,20,30,40])\n",
    "b = np.ones(4)+1\n",
    "print(a,b)\n",
    "c = a**b\n",
    "print(c)\n",
    "print(a<25) # 判断 数组中哪些数小于某个值\n",
    "d = a<25\n",
    "print(d.dtype)\n",
    "for i in range(a.size):\n",
    "    if d[i] == True:\n",
    "        print(a[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22d18725-d30a-41da-9d71-14d892477633",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 矩阵乘法\n",
    "a = a.reshape(2,2)\n",
    "b = b.reshape(2,2)\n",
    "c = a*b #逐元素相乘\n",
    "c_dot = np.dot(a,b) # 矩阵相乘\n",
    "c_dot2 = a.dot(b)   # 也是矩阵相乘\n",
    "print(c)\n",
    "print(c_dot)\n",
    "print(c_dot2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96c05f4a-b951-4e35-b4e6-81a0a9d94d8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 数组自身运算\n",
    "print(\"原始数组 a:\\n\", a)\n",
    "print(\"数组 a 的所有元素之和: {:.2f}\".format(np.sum(a)))\n",
    "print(\"数组 a 的最小值: {:.2f}\".format(np.min(a)))\n",
    "print(f\"数组 a 的最大值: {np.max(a)}\")\n",
    "print(\"数组 a （axis=1）求和的结果:\", np.sum(a, axis=1)) # axis就是对应维度，对于二维矩阵axis = 0 对各个行向量进行操作，sum就是每个行向量相加\n",
    "print(\"数组 a （axis=0）求和的结果:\", np.sum(a, axis=0))\n",
    "print(\"数组 a （axis=0）求最小值的结果:\", np.min(a, axis=0))\n",
    "print(\"数组 a （axis=1）求最大值的结果:\", np.max(a, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "af356235-ab8a-4327-8fbb-8ee8e58a5ab8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 2, 4)\n",
      "[[2 5 7 5]\n",
      " [8 5 4 7]]\n",
      "[[1 3 4 5]\n",
      " [8 4 7 5]\n",
      " [2 5 7 7]]\n",
      "[[4 5]\n",
      " [7 8]\n",
      " [7 7]]\n"
     ]
    }
   ],
   "source": [
    "## 多维数组\n",
    "B = np.array([[[1,2,3,4],[1,3,4,5]],[[2,4,7,5],[8,4,3,5]],[[2,5,7,3],[1,5,3,7]]])\n",
    "print(B.shape)\n",
    "print(np.max(B,axis=0))\n",
    "print(np.max(B,axis=1))\n",
    "print(np.max(B,axis=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2664ba4-e263-4fa1-82b8-5c538d94dbde",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 数组操作\n",
    "A = np.arange(2, 14).reshape(3, 4)\n",
    "print(\"原始数组 A:\")\n",
    "print(A)\n",
    "\n",
    "print(\"最小值的索引:\", np.argmin(A))\n",
    "print(\"最大值的索引:\", np.argmax(A))\n",
    "print(\"平均值:\", np.mean(A))\n",
    "print(\"中位数:\", np.median(A))\n",
    "print(\"前缀和（1维）:\", np.cumsum(A))\n",
    "print(\"差分（2维）:\", np.diff(A))\n",
    "print(\"按行排序:\", np.sort(A))\n",
    "print(\"限制值范围（5到9）:\", np.clip(A, a_min=5, a_max=9))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37408a28-af77-4330-b4b3-bfb182e6d4d3",
   "metadata": {},
   "source": [
    "**np索引使用方法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8148c737-1d4c-4e16-be55-44b0c336e72c",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.arange(2, 14).reshape(3,4)\n",
    "# 单独的 : 代表所有数， 1 : 5 表示从第1个到第4个，左闭右开区间\n",
    "A = np.arange(2, 14).reshape(3, 4)\n",
    "print(\"原始数组 A:\")\n",
    "print(A)\n",
    "\n",
    "# 打印第三行所有数\n",
    "print(\"\\n第三行所有数:\")\n",
    "print(A[2, :])\n",
    "\n",
    "# 打印第一列所有数\n",
    "print(\"\\n第一列所有数:\")\n",
    "print(A[:, 1])\n",
    "\n",
    "# 打印第三行第2个到第3个数（左闭右开区间）\n",
    "print(\"\\n第三行第2个到第3个数:\")\n",
    "print(A[2, 1:3])\n",
    "\n",
    "# 迭代每一列\n",
    "print(\"\\n迭代每一列:\")\n",
    "for col in A.T:  # A.T 是 A 的转置\n",
    "    print(col)\n",
    "\n",
    "# 迭代每一行\n",
    "print(\"\\n迭代每一行:\")\n",
    "for row in A:\n",
    "    print(row)\n",
    "\n",
    "# 迭代每个元素\n",
    "print(\"\\n迭代每个元素:\")\n",
    "for arg in A.flat:  # A.flat 是一个迭代器\n",
    "    print(arg)\n",
    "    \n",
    "print(\"\\n将A展平\")\n",
    "print(A.flatten())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e616227e-eed5-405f-b897-b12d1a3c35fb",
   "metadata": {},
   "source": [
    "**合并数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d67b3066-1110-4ee4-8bb6-a565352eb9ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([1,2,3]).reshape(1,3)\n",
    "B = np.array([4,5,6]).reshape(1,3)\n",
    "print(np.vstack((A,B))) # vertical stack ,垂直堆叠\n",
    "print(np.hstack((A,B))) # horizontal stack,水平堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb5d0d21-56c2-47a6-a82a-b13fce1e4c66",
   "metadata": {},
   "outputs": [],
   "source": [
    "C = np.concatenate((A,B,A,A,B,np.vstack((A,B))),axis = 0) # 指定多个数组进行水平或者垂直方向的合并，数组的维度在合并方向需要相同,如垂直方向合并，则数组的列数必须相同\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9054dcf-034b-4494-8bb5-26c7334b790a",
   "metadata": {},
   "source": [
    "**分割数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ef21c770-823c-492f-9ec9-1f40d30fa978",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "A = np.arange(12).reshape(3,4)\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cc94c187-c615-4a0b-9c69-e0f6ce922ca2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11]])]\n",
      "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "## 等量分割\n",
    "print( np.split(A,2,axis= 1 ) ) # 第二个参数是要将整体等分成几份（每一份的维度是相同的）\n",
    "print( np.split(A,3,axis= 0 ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "de2eded3-ccb4-4d39-9c52-333f7b651f35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2],\n",
      "       [ 6],\n",
      "       [10]]), array([[ 3],\n",
      "       [ 7],\n",
      "       [11]])]\n"
     ]
    }
   ],
   "source": [
    "## 不等量分割\n",
    "print( np.array_split(A,3,axis= 1))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78f8b806-004e-4774-b5d0-5f5865073dde",
   "metadata": {},
   "source": [
    "**copy**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d4ac43e4-b70c-4be3-a11c-9ca8b1f2624c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "## python中的赋值，是直接赋值（类似于C语言的指针传递，左值右值是同一个变量，修改其中一个，另一个会同步修改）\n",
    "a = np.arange(4)\n",
    "b = a\n",
    "print(b is a)\n",
    "\n",
    "## 使用copy\n",
    "b = a.copy()\n",
    "print(b is a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6d88282-d37a-4047-9c27-0668d77a2e98",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
